package cn.fl.settlement.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapCstStmApplyQC;
import cn.fl.finance.constant.FinLoanConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.dto.PrjPrdPaySchDDTO;
import cn.fl.settlement.service.ISettleBillService;
import cn.fl.settlement.service.ISettleFeeService;
import cn.fl.settlement.vo.CapCstStmApplyVO;
import cn.fl.settlement.vo.PaySchDSettlePageVO;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @Description:    结清业务控制层new
* @Author:         liurong
* @CreateDate:     2020/11/24 15:29
* @Version:        1.0
*/
@Controller
@RequestMapping("/settlement/fee")
public class SettleFeeController extends BaseController {

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Autowired
    private ISettleFeeService settleFeeService;

    /**
     * 附加页面查询结清项列表数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/initSettleItemList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initSettleItemList(HttpServletRequest request) {
        String pInstId = request.getParameter("pInstId");

        try {
            if (StringUtils.isEmpty(pInstId)) {
                throw new BaseException("流程实例ID不能为空！");
            }

            List<PaySchDSettlePageVO> settleItemList = settleFeeService.searchSettleItemList(null, pInstId);

            ListPageVO<PaySchDSettlePageVO> listPage = new ListPageVO<>(settleItemList, settleItemList.size());

            return listPage;
        } catch (BaseException e) {
            BaseLogUtils.newLogger("操作人{0},查询流程实例ID{1}结清费用项列表数据业务异常：", CurrentThreadContext.getCurrentUserName(), pInstId).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("操作人{0},查询流程实例ID{1}结清费用项列表数据系统异常：", CurrentThreadContext.getCurrentUserName(), pInstId).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    /**
     * 到结清费用项维护页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/manageListPage.do", method = {RequestMethod.GET})
    public ModelAndView manageListPage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("settlement/common/settleFeeManagePage");
        modelAndView.addObject("paySchId", request.getParameter("paySchId"));
        modelAndView.addObject("pInstId", request.getParameter("pInstId"));
        return modelAndView;
    }

    /**
     * 加载结清费用项列表数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/initSettleFeeList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initSettleFeeList(HttpServletRequest request) {
        String paySchIdStr = request.getParameter("paySchId");

        try {
            if (StringUtils.isEmpty(paySchIdStr)) {
                throw new BaseException("支付表ID不能为空！");
            }

            PrjPrdPaySchDQueryCondition condition = new PrjPrdPaySchDQueryCondition();
            condition.setPaySchId(Long.parseLong(paySchIdStr));
            List<PaySchDSettlePageVO> stlFeeList = settleFeeService.searchSettleFeeList(condition);

            ListPageVO<PaySchDSettlePageVO> listPage = new ListPageVO<>(stlFeeList, stlFeeList.size());

            return listPage;
        } catch (BaseException e) {
            BaseLogUtils.newLogger("操作人{0},查询支付表{1}结清费用项列表数据业务异常：", CurrentThreadContext.getCurrentUserName(), paySchIdStr).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("操作人{0},查询支付表{1}结清费用项列表数据系统异常：", CurrentThreadContext.getCurrentUserName(), paySchIdStr).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    /**
     * 跳转新增或编辑结清费用项页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/toAddOrEditPage.do", method = {RequestMethod.GET})
    public ModelAndView toAddOrEditPage(HttpServletRequest request) {

        List<KeyValueVO> settleFeeDicList = sysDictionaryApiService.getKvList(DictionaryConstant.CODE_PRJ_QUAT_FEE_SETT_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        String paySchId = request.getParameter("paySchId");
        String id = request.getParameter("id");
        String pInstId = request.getParameter("pInstId");
        ModelAndView modelAndView = new ModelAndView("settlement/common/settleFeeAddOrEdit");
        if (StringUtils.isNotEmpty(id)) {
            PrjPrdPaySchDQueryCondition condition = new PrjPrdPaySchDQueryCondition();
            condition.setId(Long.parseLong(id));
            condition.setPaySchId(Long.parseLong(paySchId));
            PaySchDSettlePageVO paySchDSettle = settleFeeService.querySettleFeeById(condition);

            modelAndView.addObject("fee", paySchDSettle);
        }

        modelAndView.addObject("paySchId", paySchId);
        modelAndView.addObject("pInstId", pInstId);
        modelAndView.addObject("settleFeeDic", JSON.toJSONString(settleFeeDicList));
        return modelAndView;

    }

    /**
     * 新增或修改结清费用项
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/addOrEditSettleFee.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addOrEditSettleFee(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);

        String paySchIdStr = request.getParameter("paySchId");
        String id = request.getParameter("id");
        String feeTypCd = request.getParameter("feeTypCd");
        String feeAmt = request.getParameter("feeAmt");
        String pInstId = request.getParameter("pInstId");
        try {
            if (StringUtils.isEmpty(paySchIdStr)) {
                throw new BaseException("支付表ID不能为空！");
            }
            if (StringUtils.isEmpty(pInstId)) {
                throw new BaseException("流程实例ID不能为空！");
            }
            PrjPrdPaySchDDTO entity = new PrjPrdPaySchDDTO();
            entity.setPaySchId(Long.parseLong(paySchIdStr));
            entity.setFeeTypCd(feeTypCd);
            entity.setFeeAmt(DecimalUtils.convertYuanToFen(Double.parseDouble(feeAmt)).doubleValue());
            if (StringUtils.isEmpty(id)) {
                if (StringUtils.isEmpty(feeTypCd)) {
                    throw new BaseException("新增费用项类型不能为空！");
                }
                if (StringUtils.isEmpty(feeAmt)) {
                    throw new BaseException("新增费用金额不能为空！");
                }
                // 新增
                settleFeeService.addSettleFeeFlow(pInstId, entity);
                result.setInfo(SysLogConstant.LOG_CMM_ADD_SUCC);
            } else {
                // 修改
                entity.setId(Long.parseLong(id));
                settleFeeService.modifySettleFeeFlow(pInstId, entity);
                result.setInfo(SysLogConstant.LOG_CMM_UPDATE_SUCC);
            }
        } catch (BaseException e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("保存结清费用项数据失败！" + e.getMessage());
            BaseLogUtils.newLogger("操作人{0}保存结清费用项业务异常！", CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("保存结清费用项数据失败！" + e.getMessage());
            BaseLogUtils.newLogger("操作人{0}保存结清费用项系统异常！", CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
        }
        return result;
    }

    /**
     * 删除结清费用项
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/removeSettleFee.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object removeSettleFee(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);

        String paySchIdStr = request.getParameter("paySchId");
        String id = request.getParameter("id");
        String feeTypCd = request.getParameter("feeTypCd");
        String pInstId = request.getParameter("pInstId");

        try {
            if (StringUtils.isEmpty(id)) {
                throw new BaseException("费用项ID不能为空！");
            }
            if (StringUtils.isEmpty(pInstId)) {
                throw new BaseException("流程实例ID不能为空！");
            }
            if (StringUtils.isEmpty(paySchIdStr)) {
                throw new BaseException("支付表ID不能为空！");
            }
            if (StringUtils.isEmpty(feeTypCd)) {
                throw new BaseException("费用项类型不能为空！");
            }
            // 移除
            PrjPrdPaySchDDTO entity = new PrjPrdPaySchDDTO();
            entity.setId(Long.parseLong(id));
            entity.setPaySchId(Long.parseLong(paySchIdStr));
            entity.setFeeTypCd(feeTypCd);
            settleFeeService.removeSettleFeeFlow(pInstId, entity);
        } catch (BaseException e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("删除结清费用项数据失败！" + e.getMessage());
            BaseLogUtils.newLogger("操作人{0}删除结清费用项业务异常！", CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("删除结清费用项数据失败！" + e.getMessage());
            BaseLogUtils.newLogger("操作人{0}删除结清费用项系统异常！", CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
        }
        return result;
    }





}

